package me.zhengjie.modules.vip.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.vip.domain.MemberCards;
import me.zhengjie.modules.vip.domain.RechargeRecord;
import me.zhengjie.modules.vip.service.MemberCardsService;
import me.zhengjie.modules.vip.domain.vo.MemberCardsQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.vip.domain.dto.RechargeDTO;

/**
* @author ck
* @date 2024-12-27
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "会员卡管理管理")
@RequestMapping("/api/memberCards")
public class MemberCardsController {

    private final MemberCardsService memberCardsService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('memberCards:list')")
    public void exportMemberCards(HttpServletResponse response, MemberCardsQueryCriteria criteria) throws IOException {
        memberCardsService.download(memberCardsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询会员卡管理")
    @ApiOperation("查询会员卡管理")
    @PreAuthorize("@el.check('memberCards:list')")
    public ResponseEntity<PageResult<MemberCards>> queryMemberCards(MemberCardsQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(memberCardsService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增会员卡管理")
    @ApiOperation("新增会员卡管理")
    @PreAuthorize("@el.check('memberCards:add')")
    public ResponseEntity<Object> createMemberCards(@Validated @RequestBody MemberCards resources){
        memberCardsService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改会员卡管理")
    @ApiOperation("修改会员卡管理")
    @PreAuthorize("@el.check('memberCards:edit')")
    public ResponseEntity<Object> updateMemberCards(@Validated @RequestBody MemberCards resources){
        memberCardsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除会员卡管理")
    @ApiOperation("删除会员卡管理")
    @PreAuthorize("@el.check('memberCards:del')")
    public ResponseEntity<Object> deleteMemberCards(@RequestBody List<Long> ids) {
        memberCardsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/member/{memberId}")
    @Log("获取会员卡列表")
    @ApiOperation("获取会员卡列表")
    @PreAuthorize("@el.check('memberCards:list')")
    public ResponseEntity<List<MemberCards>> getMemberCards(@PathVariable Long memberId) {
        return new ResponseEntity<>(memberCardsService.getMemberCards(memberId), HttpStatus.OK);
    }

    @PostMapping("/recharge")
    @Log("会员卡充值")
    @ApiOperation("会员卡充值")
    @PreAuthorize("@el.check('memberCards:edit')")
    public ResponseEntity<Object> rechargeCard(@Validated @RequestBody RechargeDTO rechargeDTO) {
        memberCardsService.recharge(rechargeDTO);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}